package yxy.game.pm2.active.command.guanyurecruit;

import yxy.cherry.data.bean.actrecruitcharge;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.guanyurecruit.ActiveModule_关羽招募;
import yxy.game.pm2.active.module.guanyurecruit.GYRecrit;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.ActiveMessage.ActGuanYuRecruitRequest;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 关羽招募
 * @author Dell
 */
@MessageType(proto = Command.ActGuanYuRecruit, classtype = ActiveMessage.ActGuanYuRecruitRequest.class)
public class CMD_Active_关羽招募_Info extends Command<ActGuanYuRecruitRequest> {

	@Override
	public void execute(MessageSource messageSource, ActGuanYuRecruitRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActGuanYuRecruitResponse.Builder response = ActiveMessage.ActGuanYuRecruitResponse.newBuilder();
		
		ActiveModule_关羽招募 am = ActiveModule_关羽招募.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		if(am.getGymap().size() == 0) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		Map<String, actrecruitcharge> datamap = ActDataCenter.getAllData(actrecruitcharge.class, playercode);
		
		for(GYRecrit gy : am.getGymap().values()) {
			if(gy.getEnd().isBeforeNow()) {
				continue;
			}
			
			ActiveMessage.GuanYuRecruitInfo.Builder info = ActiveMessage.GuanYuRecruitInfo.newBuilder();
			info.setId(gy.getId());
			info.setGoodsid(gy.getGoodsid());
			info.setEndtime(gy.getEnd().getMillis());
			
			actrecruitcharge data = datamap.get(String.valueOf(gy.getId()));
			info.setRebate(data.getRebate()); //返利倍数
			Collection<Currency> curs = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
			info.addAllCurs(items);
			
			int canget = gy.getBuy() != null ? DrawStatus.hadget.getStatus() : gy.getLock() == null ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			info.setCanget(canget);
			
			response.addGyinfos(info);
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActGuanYuRecruitResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

}
